package vip.xiaonuo;

import cn.hutool.log.Log;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//https://jfinal.com/share/1402

@Configuration
public class ActiveRecordPluginConfig {

    private static final Log log = Log.get();

    // 以下三个信息在 src/mian/resources/application.properties配置的数据库连接信息
    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Bean
    public ActiveRecordPlugin initActiveRecordPlugin() {

        DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driver);

        // 加强数据库安全
        WallFilter wallFilter = new WallFilter();

        wallFilter.setDbType("mysql");

        druidPlugin.addFilter(wallFilter);
        // 添加 StatFilter 才会有统计数据
        druidPlugin.addFilter(new StatFilter());

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);

        arp.setShowSql(true);

        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());

        // 与 jfinal web 环境唯一的不同是要手动调用一次相关插件的start()方法
        druidPlugin.start();
        arp.start();

        log.info("** >>>> " + arp.getConfig());
        log.info("** >>>> " + ActiveRecordPluginConfig.class.getSimpleName() + " is success!");
        return arp;
    }
}
